/**
 * 机器学习实战书籍加载
 */
 import BookLoader from "../BookLoader";
 import Algorithms from "./MLInAction.json";
 import AlgorithmHelper from "./AlgorithmHelper";
 import AlgorithmImport from './algorithms/AlgorithmImport';
 
 class MLInAction extends BookLoader{
   constructor(props){
     super(props);
 
     this.bookName   = "机器学习实战";
     this.algorithms = Algorithms.Algorithms;
    }

    init(id){
      if(!id) id = this.state.algorithmList[this.state.selected].id;
      
      this.resetAlgorithm();
      if(!id) return;
  
      AlgorithmImport(id, this.setupAlgorithm, this.resetAlgorithm);   
    }
  
    resetAlgorithm(){
  
      this.setState({
        algorithmImp : undefined
      })
    }
  
    setupAlgorithm(algorithmModule){
      const algorithmImp = new algorithmModule.default();
  
      if(algorithmImp){  
        algorithmImp.init();
      }
  
      this.setState({
        algorithmImp : algorithmImp
      })
    }

    renderAlgorithmHelper(){
      return <AlgorithmHelper 
        algorithm    = {this.state.algorithmList[this.state.selected]}
        algorithmImp = {this.state.algorithmImp}
      />;
    }
 }
 
 export default MLInAction;